#pragma once
#include "Task.h"
#if (COMM_FEATURE_LIBEVENT==1)

#define HTTPSERVER_DEFAULT_COTENTTYPE	"text/plain;charset=utf-8"

namespace COMM
{
	class COMM_API HttpRequestHandler
	{
	public:
		virtual ~HttpRequestHandler(){}
		virtual void Release(){ delete this;}
		virtual bool OnRequest(const char* uri, const char *cmdType, const char* requestContent,
			StringList& responseHeaders, Buffer& responseContent) = 0;
	};
	class COMM_API HttpServer : protected Thread
	{
	public:
		bool m_dump;
		HttpRequestHandler* m_handler;
		String m_content_type;
		Task m_task;
		
		HttpServer(HttpRequestHandler* handler,
			int listenPort, const char* listenIP="0.0.0.0", 
			int timeoutSeconds=20, int threadCount=4, bool dump=false,
			const char* contentType=HTTPSERVER_DEFAULT_COTENTTYPE,
			const char* crtfile=NULL, const char* keyfile=NULL);
		~HttpServer();

		bool Start();
		void Stop();

	private:
		virtual int Process();

	private:
		void* m_evbase;
		int m_port;
		String m_ip;
		int m_timeout_seconds;
		String m_crtfile;
		String m_keyfile;
	};
}

#endif